﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using DouBanTop250.Model;
using System.Windows.Input;
using DouBanTop250.Common;
using DouBanTop250.Helper;
using System.Windows.Controls;
using System.Windows;
using DouBanTop250.Common;

namespace DouBanTop250.ViewModel
{
    public class MainViewModel:NotifyBase
    {
        public ObservableCollection<MovieModel> MovieModels { get; set; }

        public ICommand CommandLoadData { get; set; }

        public ICommand CommandCloseWindow { get; set; }

        public ICommand CommandMessageClose { get; set; }

        private string _message = "";
        public string Message {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
                this.Notify();
            } 
        }

        private int iNum = 0;

        private Grid _grid = null;
        private ScrollViewer _scViewer = null;
        private Window _window = null;

        private bool _bIsLoading = false;
        public MainViewModel(Grid grid, ScrollViewer scViewer, Window window)
        {
            MovieModels = new ObservableCollection<MovieModel>();
            CommandLoadData = new MyCommand()
            {
                actExecute = Execute,
                funcExecute = t => { return true; }
            };
            CommandCloseWindow = new MyCommand()
            {
                actExecute = CloseWindow,
                funcExecute = t => { return true; }
            };
            CommandMessageClose = new MyCommand()
            {
                actExecute = CloseMessage,
                funcExecute = t => { return true; }
            };
            _grid = grid;
            if (_grid != null)
            {
                _grid.MouseMove += _grid_MouseMove;
            }
            _scViewer = scViewer;
            if (_scViewer != null)
            {
                _scViewer.ScrollChanged += _scViewer_ScrollChanged;
            }
            _window = window;
            if (_window != null)
            {
                _window.Loaded += _window_Loaded;
            }
        }
        private void CloseMessage(object obj)
        {
            Message = "";
        }
        private void _window_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadDefult();
        }

        private void _scViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (iNum > 9|| _bIsLoading)//最多十页
            {
                return;
            }
            var scrollViewer = e.OriginalSource as ScrollViewer;
            if (e.VerticalOffset != 0 && e.VerticalOffset == scrollViewer.ScrollableHeight)
            {
                
                iNum++;
                LoadData();
            }
        }

        private void _grid_MouseMove(object sender, MouseEventArgs e)
        {
            Grid gd = sender as Grid;
            if (gd.Parent is Window w&&e.LeftButton == MouseButtonState.Pressed)
            {
                w.DragMove();
            }
        }

        private void CloseWindow(object obj) {
            if (obj is MainWindow window)
            {
                window.Close();
            }
        }

        public void LoadDefult()
        {
            iNum = 0;
            MovieModels.Clear();
            LoadData();
        }
        private void Execute(object obj)
        {
            MovieModels.Clear();
            iNum = 0;
            LoadData();
        }
        private void LoadData()
        {
            Message = "";
            for (int i = 0; i < 10; i++)
            {
                MovieModel movieModel = new MovieModel
                {
                    ShowShadow = true
                };
                MovieModels.Add(movieModel);
            }
            
            Task.Run(new Action(async () =>
            {
                _bIsLoading = true;
                string strMsg = "";
                try
                {
                     strMsg = await HttpInfoHelper.GetResponseStringAsync(PageGetter.GetUrlByPageNum(iNum),
                            System.Net.Http.HttpMethod.Get, null);
                }
                catch
                {
                    Message = "网络链接失败,请检查";
                    return;
                }

                List<MovieModel> models = Model.MovieModel.ParseModel(strMsg);


                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    //MovieModels.Clear();//移除最后十个
                    for (int i = 0; i < 10; i++)
                    {
                        MovieModels.RemoveAt(MovieModels.Count-1);
                    }
                    if (models != null && models.Count > 0)
                    {
                        models.ForEach(x =>
                        {
                            x.Index = MovieModels.Count + 1;
                            MovieModels.Add(x);
                        });
                    }
                }));
                _bIsLoading = false;
            }));

        }
    }
}
